using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace TestWindowsGame2
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Random rand;
        Texture2D hmap;
        int _Nsize;
        float roughness;
        byte[,] _map;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            _Nsize = 512;
            rand = new Random();
            roughness = 0.2f;


            _map = new byte[_Nsize + 1, _Nsize + 1];
            _map[0, 0] = 127;
            _map[_Nsize, 0] = 127;
            _map[0, _Nsize] = 127;
            _map[_Nsize, _Nsize] = 127;
            hmap = new Texture2D(graphics.GraphicsDevice, _Nsize + 1, _Nsize + 1);
            Color[] mapColors = 
            {
                new Color(90,120,150),
                new Color(100,130,170),
                /*new Color(123,123,123),
                new Color(123,123,123),
                new Color(123,123,123),
                new Color(123,123,123),
                new Color(123,123,123),
                new Color(123,123,123),
                new Color(123,123,123),
                new Color(123,123,123),
                new Color(123,123,123),
                new Color(123,123,123)*/
            };

            Color[] mapThreshold =
            {
                Color.Black,
                Color.White
            };

            base.Initialize();

            mapGen(0, 0, _Nsize);
            createTexture(_map, hmap);
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here

            spriteBatch.Begin();
            spriteBatch.Draw(hmap, Vector2.Zero, Color.White);
            spriteBatch.End();

            base.Draw(gameTime);
        }

        public void mapGen(int x, int y, int w)
        {
            int mydelta = rand.Next((int)(-w/4), (int)(w /4));

            _map[x + w / 2, y] = (byte)((_map[x, y] + _map[x + w, y]) / 2);
            _map[x, y + w / 2] = (byte)((_map[x, y] + _map[x, y + w]) / 2);
            _map[x + w, y + w / 2] = (byte)((_map[x + w, y] + _map[x + w, y + w]) / 2);
            _map[x + w / 2, y + w] = (byte)((_map[x, y + w] + _map[x + w, y + w]) / 2);
            _map[x + w / 2, y + w / 2] = (byte)((_map[x + w / 2, y] + _map[x, y + w / 2]
                                       + _map[x + w, y + w / 2] 
                                       + _map[x + w / 2, y + w / 2]) / 4);

            if (w >= 2)
            {
                mapGen(0, 0, w / 2);
                mapGen(w / 2, 0, w / 2);
                mapGen(0, w / 2, w / 2);
                mapGen(w / 2, w / 2, w / 2);
            }
        }

        void createTexture(byte[,] _source, Texture2D _destination)
        {
            Color[] _col = new Color[_destination.Width * _destination.Height];

            int pos = 0;
            for (int i = 0; i < _destination.Width; i++)
            {
                for (int j = 0; j < _destination.Height; j++)
                {
                    _col[pos] = new Color(_source[i, j], _source[i, j], _source[i, j]);
                    pos++;
                }
            }
            _destination.SetData<Color>(_col);
        }
    }
}
